package com.xzy.urp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.xzy.urp.listener.StudentExcelListener;
import com.xzy.urp.listener.TeacherExcelListener;
import com.xzy.urp.pojo.dto.ExcelStudentDTO;
import com.xzy.urp.pojo.dto.ExcelTeacherDTO;
import com.xzy.urp.pojo.entity.CourseInfo;
import com.xzy.urp.pojo.entity.CourseTime;
import com.xzy.urp.pojo.entity.StudentInfo;
import com.xzy.urp.mapper.StudentInfoMapper;
import com.xzy.urp.pojo.entity.UserInfo;
import com.xzy.urp.pojo.vo.CourseTimeVO;
import com.xzy.urp.pojo.vo.StudentInfoVO;
import com.xzy.urp.service.CourseInfoService;
import com.xzy.urp.service.CourseTimeService;
import com.xzy.urp.service.StudentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzy.urp.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzy
 * @since 2022-02-17
 */
@Service
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo> implements StudentInfoService {

    private final UserInfoService userInfoService;
    private final CourseInfoService courseInfoService;
    private final CourseTimeService courseTimeService;

    @Autowired
    public StudentInfoServiceImpl(UserInfoService userInfoService,
                                  CourseInfoService courseInfoService,
                                  CourseTimeService courseTimeService) {
        this.userInfoService = userInfoService;
        this.courseInfoService = courseInfoService;
        this.courseTimeService = courseTimeService;
    }

    @Override
    public boolean addSInfo(StudentInfoVO studentInfoVO) {
        UserInfo userInfo = userInfoService.getById(studentInfoVO.getUId());
        StudentInfo studentInfo = new StudentInfo();
        if(userInfo!=null){
            BeanUtil.copyProperties(studentInfoVO,studentInfo);
            baseMapper.insert(studentInfo);
            return  true;
        }
        return false;
    }

    @Override
    public StudentInfo getInfoById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<CourseTimeVO> getCTimeById(Long id, Integer year) {
        return courseTimeService.getByStudentId(id,year);
    }

    @Override
    public void importData(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelStudentDTO.class,new StudentExcelListener(this)).sheet().doRead();
    }
}
